#include "addwidget.h"
#include "database.hpp"
#include "ui_addwidget.h"
#include <QCalendarWidget>
#include <QDebug>
#include <QMessageBox>
#include <QPushButton>

extern CDatabase *m_db;

AddWidget::AddWidget(QWidget *parent, int type)
    : QDialog(parent)
    , ui(new Ui::AddWidget)
{
    ui->setupUi(this);

    //退出按键文字
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText("确定");
    ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("取消");

    //无标题栏,使圆角会透明
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    //父窗口灰色遮罩
    widget1 = new QWidget(parent, Qt::FramelessWindowHint);
    widget1->resize(parent->size());
    widget1->setObjectName("widget_parent"); // 设置对象句,相当于css里的id
    widget1->setStyleSheet("#widget_parent {background-color:rgba(160, 160, 160,210);}"); // 设置id对应元素的背景色
    widget1->show();

    //初始化时间
    ui->dateEdit_start->setDate(QDate::currentDate());
    ui->dateEdit_stop->setDate(QDate::currentDate().addMonths(3));

    //////////////////////////////////////////////////
    m_getType = type;
    if (type == PROJECT)
    {
        ui->label_Project->setHidden(true);
        ui->label_Eipc->setHidden(true);
        ui->label_Feature->setHidden(true);
        ui->label_Story->setHidden(true);

        ui->comboBox_Project->setHidden(true);
        ui->comboBox_Eipc->setHidden(true);
        ui->comboBox_Feature->setHidden(true);
        ui->comboBox_Story->setHidden(true);
    }
    else if (type == EPIC)
    {
        ui->label_Project->setHidden(false);
        ui->label_Eipc->setHidden(true);
        ui->label_Feature->setHidden(true);
        ui->label_Story->setHidden(true);

        ui->comboBox_Project->setHidden(false);
        ui->comboBox_Eipc->setHidden(true);
        ui->comboBox_Feature->setHidden(true);
        ui->comboBox_Story->setHidden(true);
    }
    else if (type == FEATURE)
    {
        ui->label_Project->setHidden(false);
        ui->label_Eipc->setHidden(false);
        ui->label_Feature->setHidden(true);
        ui->label_Story->setHidden(true);

        ui->comboBox_Project->setHidden(false);
        ui->comboBox_Eipc->setHidden(false);
        ui->comboBox_Feature->setHidden(true);
        ui->comboBox_Story->setHidden(true);
    }
    else if (type == STORY)
    {
        ui->label_Project->setHidden(false);
        ui->label_Eipc->setHidden(false);
        ui->label_Feature->setHidden(false);
        ui->label_Story->setHidden(true);

        ui->comboBox_Project->setHidden(false);
        ui->comboBox_Eipc->setHidden(false);
        ui->comboBox_Feature->setHidden(false);
        ui->comboBox_Story->setHidden(true);
    }
    else
    {
        ui->time->setHidden(true);
    }

    if (type != PROJECT)
        fushComboBox(ui->comboBox_Project);
}

AddWidget::~AddWidget()
{
    delete widget1;
    delete ui;
}

void AddWidget::accept()
{
    if (ui->label->text().length() == 0
        || ui->textEdit->toPlainText().length() == 0)
    {
        QDialog::accept();
    }
    else
    {
        QMessageBox::critical(0, "错误", "没有父对象，不能在此创建");
    }
}

int m_pid;
QList<QStringList> comboBoxTxt = { {}, {}, {}, {} };

void AddWidget::fushComboBox(QComboBox *currentComboBox)
{
    QList<QComboBox *> arrr = {
        ui->comboBox_Project, //标题类型0
        ui->comboBox_Eipc, //标题类型1
        ui->comboBox_Feature, //标题类型2
        ui->comboBox_Story //标题类型3
    };
    QComboBox *comboBox = nullptr;
    int n = arrr.indexOf(currentComboBox);
    int nn = currentComboBox->currentIndex();

    //是隐藏的
    if (currentComboBox->isHidden())
        return;

    //    //是最后一个
    //    if (currentComboBox == arrr.last())
    //        return;

    int nextTitlesType, nextTitlesPid;
    if (currentComboBox == arrr.first() && currentComboBox->count() == 0) //初始化
    {
        nextTitlesType = 0;
        nextTitlesPid = 0;
        comboBox = currentComboBox;
    }
    else //选择改变
    {
        qDebug() << currentComboBox->objectName() << currentComboBox->currentText();

        // 不退出，否则不能清空后面继承的comboboc
        //        if (currentComboBox->currentText().length() == 0)
        //            return;

        if (currentComboBox->currentText().length() > 0)
        {
            m_pid = comboBoxTxt[n][nn].split("##_").at(0).toInt();
            ui->dateEdit_start->setDate(QDateTime::fromString(comboBoxTxt[n][nn].split("##_").at(2), "yyyyMMddhhmmss").date());
            ui->dateEdit_stop->setDate(QDateTime::fromString(comboBoxTxt[n][nn].split("_").at(3), "yyyyMMddhhmmss").date());
        }

        //是最后一个
        if (currentComboBox == arrr.last())
            return;

        //下一个节点是隐藏的
        comboBox = arrr.at(n + 1);
        if (comboBox->isHidden())
        {
            qDebug() << comboBox->objectName() << "isHidden";
            return;
        }

        //下一组标题的筛选消息
        nextTitlesType = n + 1;

        if (currentComboBox->currentText().length() == 0)
            nextTitlesPid = 0;
        else
            nextTitlesPid = comboBoxTxt[n][nn].split("##_").at(0).toInt();
    }

    //搜索指定类型和pid的标题添加到下一个下拉框
    qDebug() << "find:" << nextTitlesPid << "-" << nextTitlesType;
    QStringList infos, titles;
    ui->label->clear();
    comboBox->clear();
    m_db->getTitleByParent(nextTitlesPid, nextTitlesType, infos, titles);
    if (titles.length() == 0)
    {
        ui->label->setText("没有父对象，不能在此创建！");
        qDebug() << "not data!";
        return;
    }

    qDebug() << infos;
    comboBoxTxt[nextTitlesType] = infos; //先与addItems，addItems会触发comboBox修改，进而再次进入本函数
    //    qDebug() << comboBoxTxt;
    comboBox->addItems(titles);
    return;
}

void AddWidget::on_comboBox_Project_currentIndexChanged(const QString &arg1)
{
    (void)arg1;
    fushComboBox(ui->comboBox_Project);
    return;
}

void AddWidget::on_comboBox_Eipc_currentIndexChanged(const QString &arg1)
{
    (void)arg1;
    fushComboBox(ui->comboBox_Eipc);
    return;
}

void AddWidget::on_comboBox_Feature_currentIndexChanged(const QString &arg1)
{
    (void)arg1;
    fushComboBox(ui->comboBox_Feature);
    return;
}

void AddWidget::on_comboBox_Story_currentIndexChanged(const QString &arg1)
{
    (void)arg1;
    fushComboBox(ui->comboBox_Story);
    return;
}

void AddWidget::getAttrs(uint64_t &startTime, uint64_t &stopTime, int &parentId)
{
    if (m_getType == 0)
        parentId = 0;
    else if (ui->textEdit->toPlainText().length() == 0)
        parentId = -1;
    else
        parentId = m_pid;
    //    qDebug() << parentId;

    //用户选择时间
    startTime = ui->dateEdit_start->date().toString("yyyyMMdd000000").toULongLong();
    stopTime = ui->dateEdit_stop->date().toString("yyyyMMdd235959").toULongLong();
}

QStringList AddWidget::getTitles()
{
    QString s;
    QStringList titles;
    QStringList tmp = ui->textEdit->toPlainText().split("\n");
    for (int i = 0; i < tmp.length(); i++)
    {
        s = tmp.at(i).simplified(); //去除多余的空格
        if (s.length() != 0 && s != " ")
            titles.append(s);
    }

    return titles;
}
